package MyFragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ProgressBar;
import android.widget.Toast;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import SQJavaBean.FirstSQshouye;
import SQJavaBean.SecondSQxiangqing;
import SQJavaBean.TitleSQ;
import pushe.qiongyou.MyGirdViewAdapter;
import pushe.qiongyou.R;
import pushe.qiongyou.SheQuSecondActivity;


public class SheQuFragment extends Fragment {
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    private ArrayList<TitleSQ> ts;
    private ArrayList<FirstSQshouye> fs;
    private ArrayList<SecondSQxiangqing> ss;
    private ProgressBar progressBar;

    public static SheQuFragment newInstance(String param1, String param2) {
        SheQuFragment fragment = new SheQuFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    public SheQuFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_she_qu, container, false);
        final GridView gridView= (GridView) view.findViewById(R.id.sq_girdview);
        progressBar = (ProgressBar) view.findViewById(R.id.sq_progress);
        ts =new ArrayList<TitleSQ>();
        fs = new ArrayList<FirstSQshouye>();
        ss = new ArrayList<SecondSQxiangqing>();


        //使用xUtils下载json,解析
        HttpUtils http=new HttpUtils();
        http.send(HttpRequest.HttpMethod.GET, "http://open.qyer.com/qyer/bbs/forum_list?client_id=qyer_android&client_secret=9fcaae8aefc4f9ac4915&v=1&track_user_id=5863963&track_deviceid=99000628714115&track_app_version=6.2&track_app_channel=wandoujia&track_device_info=cancro&track_os=Android4.4.4&app_installtime=1429441097905&lat=40.037354&lon=116.370546", new RequestCallBack<Object>() {
            @Override
            public void onSuccess(ResponseInfo<Object> objectResponseInfo) {
                progressBar.setVisibility(View.GONE);
                try {
                    JSONObject jsonObject = new JSONObject((String) objectResponseInfo.result);
                    JSONArray data = jsonObject.getJSONArray("data");
                    for (int i = 0; i < data.length(); i++) {
                        JSONObject obj1 = data.getJSONObject(i);
                        String id1 = obj1.getString("id");
                        String name1 = obj1.getString("name");
                        TitleSQ t = new TitleSQ(id1, name1);
                        ts.add(t);
                        JSONArray group = obj1.getJSONArray("group");
                        for (int j = 0; j < group.length(); j++) {
                            JSONObject obj2 = group.getJSONObject(j);
                            String id2 = obj2.getString("id");
                            String name2 = obj2.getString("name");
                            String total_threads = obj2.getString("total_threads");
                            String photo = obj2.getString("photo");
                            FirstSQshouye f = new FirstSQshouye(name2, id2, total_threads, photo);
                            fs.add(f);
                            JSONArray types = obj2.getJSONArray("types");
                            for (int k = 0; k < types.length(); k++) {
                                JSONObject obj3 = types.getJSONObject(k);
                                String id3 = obj3.getString("id");
                                String name3 = obj3.getString("name");
                                SecondSQxiangqing s = new SecondSQxiangqing(id3, name3);
                                ss.add(s);
                            }

                        }
                    }
                    MyGirdViewAdapter adapter = new MyGirdViewAdapter(fs, getActivity());
                    gridView.setAdapter(adapter);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onFailure(HttpException e, String s) {
                Toast.makeText(getActivity(), "下载失败", Toast.LENGTH_SHORT).show();
            }
        });

        //设置gridView的每一个item的点击事件
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent i= new Intent(getActivity(), SheQuSecondActivity.class);
                startActivity(i);

            }
        });
        return view;
    }



}
